package com.itheima.reggie.controller;

import com.itheima.reggie.commons.R;
import com.itheima.reggie.pojo.OrderDetail;
import com.itheima.reggie.pojo.Orders;
import com.itheima.reggie.pojo.Page;
import com.itheima.reggie.service.OrdersService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
@Api(tags ="订单类")
@Slf4j
@RestController
@RequestMapping("/order")
public class OrdersController extends BaseController{
    @Autowired
    private OrdersService orderService;

    @GetMapping("/page")
    public R<Page<Orders>> findByPage(Integer page, Integer pageSize, String number, String beginTime,String endTime){
        if (beginTime!=null && beginTime!=""){
            if (endTime!=null && endTime!=""){
                DateTimeFormatter p = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                LocalDateTime begin = LocalDateTime.parse(beginTime,p);
                LocalDateTime end = LocalDateTime.parse(endTime,p);
                Page<Orders> byPage = orderService.findByPage(page, pageSize,number,begin,end);
                return R.success(byPage);
            }
        }
            Page<Orders> byPage = orderService.findByPage(page, pageSize,number,null,null);
            return R.success(byPage);
    }
    @GetMapping("/userPage")
    public R<Page<Orders>> userPage(Integer page, Integer pageSize,String number, String beginTime,String endTime){
        DateTimeFormatter p = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime begin = LocalDateTime.parse(beginTime,p);
        LocalDateTime end = LocalDateTime.parse(endTime,p);
        Page<Orders> byPage = orderService.findByPage(page, pageSize,number,begin,end);
        return R.success(byPage);
    }
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
        log.info("订单数据：{}",orders);
        Long userId = getLoginIdFromSession();
        orders.setUserId(userId);
        orderService.submit(orders);
        return R.success("下单成功");
    }
}
